package com.duxd.springsecurityfinal.springsecurityfinal.security.config;

import com.duxd.springsecurityfinal.springsecurityfinal.annotation.NeedAuthentication;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * @author dong
 * @date 2022/1/15 0015
 * 过滤出不需要认证的controller handler
 */
public class AuthenticationRequestMappingHandlerMapping extends RequestMappingHandlerMapping{

    private Set<String> publicUrl = new HashSet<>();


    @Override
    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {

        // 获取注解上
        Annotation annotation = AnnotationUtils.getAnnotation(method, NeedAuthentication.class);
        if(annotation != null && !(Boolean)AnnotationUtils.getValue(annotation, NeedAuthentication.name)){
            publicUrl.addAll(mapping.getPatternsCondition().getPatterns());
        }

        super.registerHandlerMethod(handler, method, mapping);
    }

    public Set<String> getPublicUrl() {
        return publicUrl;
    }
}
